/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import model.Reagent;

/**
 *
 * @author William
 */
public class ReagentDao {
    public void create(Reagent reagents, EntityManager em) {
        try {
            em.persist(reagents);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public Reagent search(Long id, EntityManager em) {
        Reagent reagents = null;
        try {
            reagents = em.find(Reagent.class, id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return reagents;
    }

    public void update(Reagent reagents, EntityManager em) {
        try {
            em.merge(reagents);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void delete(Reagent reagents, EntityManager em) {
        try {
            em.remove(reagents);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public List<Reagent> listAll(EntityManager em) {
        List<Reagent> reagentsList = new ArrayList<>();
        Query q = em.createQuery("SELECT u FROM Reagent u");
        try {
            List<Reagent> reagentss = new ArrayList<Reagent>();
            reagentss = q.getResultList();
            for (Object s : reagentss) {
                reagentsList.add((Reagent) s);
            }
            return reagentsList;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return reagentsList;
    }
}
